package com.factory.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.factory.dao.raw_material;
import com.factory.mapper.RawMapper;
import com.factory.service.RawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RawApiController {
    @Autowired
    RawService rawService;

    @GetMapping("/api/RawHouse/get")
    public String get(@RequestParam("limit") Integer limit,
                      @RequestParam( value = "page",defaultValue = "1") Integer pageindex,
                      HttpSession session){
        Page<raw_material> page= new Page<>(pageindex , limit);
        IPage<raw_material> page1 = rawService.page(page);
        session.setAttribute("username","hhhh");

        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","0");
        map.put("count",page1.getTotal());
        map.put("data",page1.getRecords());
        JSONObject jsonObject = new JSONObject(map);
        return jsonObject.toString();
    }
    @PostMapping("/api/RawMaterial/post")
    public JSONObject DeleteRaw(raw_material raw){


        rawService.save(raw);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","0");
        map.put("count",0);
        map.put("data",0);
        JSONObject jsonObject = new JSONObject(map);
        return jsonObject;
    }

    @DeleteMapping("/api/RawHouse/delete")
    public JSONObject DeleteRaw(@RequestParam("id") int id){


        rawService.removeById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","0");
        map.put("count",0);
        map.put("data",0);
        JSONObject jsonObject = new JSONObject(map);
        return jsonObject;
    }

    @GetMapping("/api/RawMaterial/get/{id}")
    public raw_material get(@PathVariable("id") int id){

        return  rawService.getById(id);
    }
    @PutMapping("/api/RawMaterial/put")
    public JSONObject put(raw_material raw){

        rawService.updateById(raw);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","0");
        map.put("count",0);
        map.put("data",0);
        JSONObject jsonObject = new JSONObject(map);
        return jsonObject;
    }
}
